/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RMS;

import MD5.MD5;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author SamSung
 */
public class EstablishClientID {

    private static RecordStore rs = null;

    private static String getUniqueClientID() {
        long l = new Date().getTime();
        //convert plaintext into bytes
        byte plain[] = String.valueOf(l).getBytes();
        // create MD5 object
        MD5 md5 = new MD5(plain);
        //get the resulting hashed byte
        byte[] result = md5.doFinal();
        //convert the hashed byte into hexadecimal character for display
        String hashResult = md5.toHex(result);

        return hashResult;
    }

    public static void createRecord() {
        try {
            rs = RecordStore.openRecordStore("ClientID", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream data = new DataOutputStream(baos);
        RecordEnumeration re = null;

        try {
            re = rs.enumerateRecords(null, null, true);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }

        if (!re.hasNextElement()) {
            try {
                data.writeUTF(getUniqueClientID());
                System.out.println("ClientID=" + getUniqueClientID());
                data.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            byte[] rec = baos.toByteArray();

            try {
                rs.addRecord(rec, 0, rec.length);
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        try {
            data.close();
            baos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public static String getClientID() {
        try {
            rs = RecordStore.openRecordStore("ClientID", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        RecordEnumeration re = null;
        String clientId = null;
        try {
            re = rs.enumerateRecords(null, null, true);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }

        re.reset();
        if (re.hasNextElement()) {
            try {
                byte[] ID = rs.getRecord(1);
                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(ID));
                try {
                    clientId = dis.readUTF();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return clientId;
    }
}
